home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / odlist / odlist.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-12-06  |  11.4 KB  |  301 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "VB Messenger Owner-Draw ListBox Sample"
  5.    ClientHeight    =   5415
  6.    ClientLeft      =   6135
  7.    ClientTop       =   2970
  8.    ClientWidth     =   7755
  9.    Height          =   5820
  10.    Left            =   6075
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   5415
  14.    ScaleWidth      =   7755
  15.    Top             =   2625
  16.    Width           =   7875
  17.    Begin ListBox List1 
  18.       Height          =   2370
  19.       Left            =   180
  20.       TabIndex        =   8
  21.       Top             =   2430
  22.       Width           =   3495
  23.    End
  24.    Begin PictureBox picFile 
  25.       AutoRedraw      =   -1  'True
  26.       AutoSize        =   -1  'True
  27.       BorderStyle     =   0  'None
  28.       Height          =   195
  29.       Left            =   3300
  30.       Picture         =   ODLIST.FRX:0000
  31.       ScaleHeight     =   195
  32.       ScaleWidth      =   195
  33.       TabIndex        =   7
  34.       Top             =   5970
  35.       Width           =   195
  36.    End
  37.    Begin PictureBox picDir 
  38.       AutoRedraw      =   -1  'True
  39.       AutoSize        =   -1  'True
  40.       BorderStyle     =   0  'None
  41.       Height          =   195
  42.       Left            =   3060
  43.       Picture         =   ODLIST.FRX:00E2
  44.       ScaleHeight     =   195
  45.       ScaleWidth      =   195
  46.       TabIndex        =   6
  47.       Top             =   5970
  48.       Width           =   195
  49.    End
  50.    Begin CommandButton Command1 
  51.       Cancel          =   -1  'True
  52.       Caption         =   "End Demo"
  53.       Default         =   -1  'True
  54.       Height          =   435
  55.       Left            =   2970
  56.       TabIndex        =   1
  57.       Top             =   4890
  58.       Width           =   1575
  59.    End
  60.    Begin VBMsg VBMsg1 
  61.       Height          =   420
  62.       Left            =   7140
  63.       MessageCount    =   ODLIST.FRX:01C4
  64.       MessageList     =   ODLIST.FRX:01C6
  65.       MessageTypes    =   0  'Selected Messages
  66.       PostDefault     =   0   'False
  67.       Top             =   4830
  68.       Width           =   420
  69.    End
  70.    Begin Frame Frame1 
  71.       Caption         =   "Description"
  72.       Height          =   4665
  73.       Left            =   3810
  74.       TabIndex        =   2
  75.       Top             =   120
  76.       Width           =   3795
  77.       Begin Label Label3 
  78.          BackStyle       =   0  'Transparent
  79.          Caption         =   "This sample program uses VB Messenger to trap the WM_DRAWITEM message that gets sent to the form (the parent of the list box) and draws a picture with text for each item.  This sample also uses the Windows API extensively."
  80.          FontBold        =   0   'False
  81.          FontItalic      =   0   'False
  82.          FontName        =   "MS Sans Serif"
  83.          FontSize        =   8.25
  84.          FontStrikethru  =   0   'False
  85.          FontUnderline   =   0   'False
  86.          ForeColor       =   &H00FF0000&
  87.          Height          =   1035
  88.          Left            =   180
  89.          TabIndex        =   5
  90.          Top             =   3450
  91.          Width           =   3435
  92.       End
  93.       Begin Label Label2 
  94.          BackStyle       =   0  'Transparent
  95.          Caption         =   "When an item in the list box needs to be drawn (i.e., because of a repaint or if focus or the selection has changed), Windows sends the parent control (or form) a WM_DRAWITEM message.  With that message comes a pointer to a data structure (or Type) that contains all the information needed to draw the item (such as the rectangle in which to draw it)"
  96.          FontBold        =   0   'False
  97.          FontItalic      =   0   'False
  98.          FontName        =   "MS Sans Serif"
  99.          FontSize        =   8.25
  100.          FontStrikethru  =   0   'False
  101.          FontUnderline   =   0   'False
  102.          ForeColor       =   &H00FF0000&
  103.          Height          =   1665
  104.          Left            =   180
  105.          TabIndex        =   4
  106.          Top             =   1680
  107.          Width           =   3435
  108.       End
  109.       Begin Label Label1 
  110.          BackStyle       =   0  'Transparent
  111.          Caption         =   "This sample program uses VB Messenger to display an owner-draw list box.  Owner-draw means that all drawing of text and/or pictures that appear in the list box is drawn by the programmer, rather than automatically handled by the list box itself."
  112.          FontBold        =   0   'False
  113.          FontItalic      =   0   'False
  114.          FontName        =   "MS Sans Serif"
  115.          FontSize        =   8.25
  116.          FontStrikethru  =   0   'False
  117.          FontUnderline   =   0   'False
  118.          ForeColor       =   &H00FF0000&
  119.          Height          =   1170
  120.          Left            =   180
  121.          TabIndex        =   3
  122.          Top             =   360
  123.          Width           =   3435
  124.          WordWrap        =   -1  'True
  125.       End
  126.    End
  127.    Begin ListBox lstOwnerDraw 
  128.       Height          =   2175
  129.       Left            =   180
  130.       TabIndex        =   0
  131.       Top             =   210
  132.       Width           =   3495
  133.    End
  134. Option Explicit
  135. Sub Command1_Click ()
  136.     End
  137. End Sub
  138. Sub DrawItem (lpdis As DRAWITEMSTRUCT)
  139.     Dim rc&
  140.     Dim lpstr$
  141.     Dim hdcSource%, cx%, cy%
  142.     Dim cSelBack&
  143.     'If no items in list box yet, indicate focus for
  144.     'specified rectangle
  145.     If (lpdis.itemID = -1) Then
  146.     DrawFocusRect lpdis.hDC, lpdis.rcItem
  147.     Exit Sub
  148.     End If
  149.     'If Windows wants us to draw the entire item of just change
  150.     'the selection state, we do this stuff
  151.     If (lpdis.itemAction And ODA_DRAWENTIRE) Or (lpdis.itemAction And ODA_SELECT) Then
  152.     'If the item it is selected, fill in the rectangle
  153.     'with the system color for highlight. If not selected,
  154.     'fill the rectangle with the standard window color.
  155.     'Also, the the background and foreground colors
  156.     'appropriately based on selection.
  157.     If (lpdis.itemState And ODS_SELECTED) Then
  158.         cSelBack = GetSysColor(COLOR_HIGHLIGHT)
  159.         rc = SetBkColor(lpdis.hDC, cSelBack)
  160.         rc = SetTextColor(lpdis.hDC, GetSysColor(COLOR_HIGHLIGHTTEXT))
  161.         DrawSelectionRect lpdis, cSelBack
  162.     Else
  163.         cSelBack = GetSysColor(COLOR_WINDOW)
  164.         rc = SetBkColor(lpdis.hDC, cSelBack)
  165.         rc = SetTextColor(lpdis.hDC, GetSysColor(COLOR_WINDOWTEXT))
  166.         DrawSelectionRect lpdis, cSelBack
  167.     End If
  168.     'If the item is a directory, use the picDir picture
  169.     'else use the picFile picture.  The hDC property
  170.     'is used for drawing with the Windows API as we will
  171.     'do next.
  172.     If (lstOwnerDraw.ItemData(lpdis.itemID)) Then
  173.         hdcSource = picDir.hDC
  174.     Else
  175.         hdcSource = picFile.hDC
  176.     End If
  177.     'All Windows API calls require that coordinates and sizes
  178.     'be in pixels.
  179.     cx = picDir.Width / Screen.TwipsPerPixelX
  180.     cy = picDir.Height / Screen.TwipsPerPixelY
  181.     'This function copies the image in the picture box
  182.     'to an area specified
  183.     rc = BitBlt(lpdis.hDC, lpdis.rcItem.left, lpdis.rcItem.top, cx, cy, hdcSource, 0, 0, SRCCOPY)
  184.     'Now, draw the text using the DrawText Windows API
  185.     lpdis.rcItem.left = lpdis.rcItem.left + cx + 5
  186.     lpstr = lstOwnerDraw.List(lpdis.itemID)
  187.     rc = DrawText(lpdis.hDC, lpstr, Len(lpstr), lpdis.rcItem, DT_VCENTER Or DT_SINGLELINE)
  188.         
  189.     'if item has focus, do additional drawing -- dashed border
  190.     If (lpdis.itemState And ODS_FOCUS) Then
  191.         DrawFocusRect lpdis.hDC, lpdis.rcItem
  192.     End If
  193.     Exit Sub
  194.     End If
  195.     'If only focus has changed, display or hide the focus rectangle.
  196.     'DrawFocusRect will display the dotted rectangle if one isn't
  197.     'already there, otherwise if there is one it will clear it.
  198.     If (lpdis.itemAction And ODA_FOCUS) Then
  199.     DrawFocusRect lpdis.hDC, lpdis.rcItem
  200.     Exit Sub
  201.     End If
  202. End Sub
  203. 'This routine fills an area (a rectangle) with a color
  204. Sub DrawSelectionRect (lpdis As DRAWITEMSTRUCT, cSelBack As Long)
  205.     Dim rc&
  206.     Dim hbrSel%, hbrOld%
  207.     hbrSel = CreateSolidBrush(cSelBack)
  208.     hbrOld = SelectObject(lpdis.hDC, hbrSel)
  209.     rc = FillRect(lpdis.hDC, lpdis.rcItem, hbrSel)
  210.     rc = SelectObject(lpdis.hDC, hbrOld)
  211.     rc = DeleteObject(hbrSel)
  212. End Sub
  213. Sub Form_Load ()
  214.     Dim lpm As MODEL
  215.     Dim rc&
  216.     Dim attr%, f$, Path$
  217.     Dim lSaveStyle&
  218.     'Subclass the form since the form gets the child controls'
  219.     'WM_DRAWITEM messages
  220.     VBMsg1 = hWnd
  221.     'The control model is what a VBX uses to keep track of
  222.     'internal information such as window styles
  223.     rc = ptGetControlModel(lstOwnerDraw, lpm)
  224.     lSaveStyle = lpm.flWndStyle
  225.     lpm.flWndStyle = lpm.flWndStyle Or LBS_OWNERDRAWFIXED Or LBS_HASSTRINGS
  226.     ptSetControlModel lstOwnerDraw, lpm
  227.     'since we changed the style, we need to recreate the list box
  228.     rc = ptRecreateControlHwnd(lstOwnerDraw)
  229.     'Now we need to reset the style so other list boxes
  230.     'will be normal
  231.     rc = ptGetControlModel(lstOwnerDraw, lpm)
  232.     lpm.flWndStyle = lSaveStyle
  233.     ptSetControlModel lstOwnerDraw, lpm
  234.     'when you recreate a window, it becomes invisible so
  235.     'we need to reset the Visible property
  236.     lstOwnerDraw.Visible = True
  237.     'Load our list box with data
  238.     'This example loads the list box with files and directories
  239.     'of the root of the current drive
  240.     attr = ATTR_READONLY Or ATTR_HIDDEN Or ATTR_SYSTEM Or ATTR_DIRECTORY
  241.     Path = "\"
  242.     f = Dir(Path + "*.*", attr)
  243.     Do While f <> ""
  244.     lstOwnerDraw.AddItem LCase(f)
  245.     List1.AddItem LCase(f)
  246.     If GetAttr(Path + f) And ATTR_DIRECTORY Then
  247.         lstOwnerDraw.ItemData(lstOwnerDraw.NewIndex) = True
  248.     Else
  249.         lstOwnerDraw.ItemData(lstOwnerDraw.NewIndex) = False
  250.     End If
  251.     f = Dir
  252.     Loop
  253. End Sub
  254. Sub VBMsg1_WindowMessage (hWindow As Integer, msg As Integer, wParam As Integer, lParam As Long, RetVal As Long, CallDefProc As Integer)
  255.     Select Case msg
  256.     'WM_MEASUREITEM is sent to the parent window (the form)
  257.     'so that we can return the dimensions of each item in the
  258.     'list box.  This list box was created using the
  259.     'LBS_OWNERDRAWFIXED style.  Therefore, this message is
  260.     'only sent once to get the dimensions of all items.
  261.     'if instead we used the LBS_OWNERDRAWVARIABLE style,
  262.     'this message would be sent for each item in the list.
  263.     Case WM_MEASUREITEM
  264.         
  265.         'MEASUREITEMSTRUCT contains information on the size
  266.         'of each item in the list.
  267.         Dim lpmis As MEASUREITEMSTRUCT
  268.         
  269.         'This VBMSG internal API copies data at an address
  270.         '(which in this case is stored in lParam) to a Type
  271.         'variable
  272.         ptGetTypeFromAddress lParam, lpmis, Len(lpmis)
  273.         
  274.         'Set the items' height to 13 pixels
  275.         lpmis.itemHeight = 13
  276.         'now write the structure back to the address
  277.         ptCopyTypeToAddress lParam, lpmis, Len(lpmis)
  278.         
  279.         'Always return TRUE and don't call the Windows default processing
  280.         RetVal = True
  281.         CallDefProc = False
  282.     'This message is sent to draw a specific item in the list.
  283.     Case WM_DRAWITEM
  284.         'DRAWITEMSTRUCT contains information on how to draw
  285.         'the item in the list such as the selection/focus state
  286.         'and the rectangle
  287.         Dim lpdis As DRAWITEMSTRUCT
  288.         
  289.         'This VBMSG internal API copies data at an address
  290.         '(which in this case is stored in lParam) to a Type
  291.         'variable
  292.         ptGetTypeFromAddress lParam, lpdis, Len(lpdis)
  293.         'Call the routine that draws the entier item
  294.         DrawItem lpdis
  295.         
  296.         'Always return TRUE and don't call the Windows default processing
  297.         RetVal = True
  298.         CallDefProc = False
  299.     End Select
  300. End Sub
  301.